package com.hs.srm.admin.modules.warehouse.controller;

import com.hs.srm.admin.modules.orders.entity.DeliverEntity;
import com.hs.srm.admin.modules.orders.entity.StockEntity;
import com.hs.srm.admin.modules.orders.service.StockService;
import com.hs.srm.admin.modules.sys.controller.AbstractController;
import com.hs.srm.admin.modules.warehouse.service.ReceiptService;
import com.hs.srm.common.utils.R;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @Description: 收货实现控制层
 * @Author socik
 * @date 2019/5/5 14:39
 */
@RestController
@RequestMapping("/warehouseReceipt")
public class ReceiptController extends AbstractController {
    private static final long serialVersionUID = 1L;

    @Autowired
    private ReceiptService receiptServiceImpl;
    @Autowired
    private StockService stockServiceImpl;

    /**
     * @Description receiptOperation
     * @Param [QRcode]
     * @Return com.hs.srm.common.utils.R
     * @Author socik
     * @Date 2019/5/5 14:39
     */
    @RequestMapping(value = "/receiptOperation")
    public R receiptOperation(String QRcode) throws IllegalAccessException, NoSuchMethodException,
            InvocationTargetException {

        List<DeliverEntity> list = this.receiptServiceImpl.selectByFph(QRcode);
        List<StockEntity> listStock = this.stockServiceImpl.selectStockByFph(QRcode);
        //先判断有没有这个发货单号
        if (list != null) {
            //判断有没有收货
            if (listStock.size() == 0) {
                for (DeliverEntity deliver : list) {
                    StockEntity stock = new StockEntity();
                    deliver.setStatus(null);
                    deliver.setPkId(null);
                    PropertyUtils.copyProperties(stock, deliver);
                    stock.setStatus(StockEntity.YI_SHOU_HUO);
                    boolean success = stockServiceImpl.insertStock(stock);
                    //成功插入收货表数据
                    if (success == true) {
                        continue;
                    }
                    return R.ok(2);
                }
                return R.ok(0);
            }
        }
        return R.ok(1);
    }

}
